#include "cameraadjust.h"
#include "ui_cameraadjust.h"
#include "appConfig.h"
#include <QRegExpValidator>
#include <QDebug>
#include <QMessageBox>
#include <QTimer>

extern t_system_data sysData;

cameraAdjust::cameraAdjust(QWidget *parent, QSerialPort *mPort) :
    QWidget(parent),
    ui(new Ui::cameraAdjust)
{
    meWidget = parent;
    port = mPort;
    meWidget->setGeometry(10,70,780,500);
    ui->setupUi(this);
    listSpeed << "1S" << "3S"  << "5S";
    listStatus << "关闭" << "开启";
    ui->lineEdit->setValidator(new QRegExpValidator(QRegExp("[0-9%]+$")));
    ui->lineEdit_2->setValidator(new QRegExpValidator(QRegExp("[0-9%]+$")));

    zoomBtGrop = new QButtonGroup(this);
    zoomBtGrop->addButton(ui->radioButton,0);
    zoomBtGrop->addButton(ui->radioButton_2,1);
    zoomBtGrop->addButton(ui->radioButton_3,2);
    zoomBtGrop->addButton(ui->radioButton_4,3);
    zoomBtGrop->addButton(ui->radioButton_5,4);
    zoomBtGrop->setExclusive(true);
    ui->radioButton->setChecked(true);
    speedBtGrop = new QButtonGroup(this);
    speedBtGrop->addButton(ui->radioButton_6,0);
    speedBtGrop->addButton(ui->radioButton_7,1);
    speedBtGrop->addButton(ui->radioButton_8,2);

    zoomStyleGrop = new QButtonGroup(this);
    zoomStyleGrop->addButton(ui->pushButton_3,0);
    zoomStyleGrop->addButton(ui->pushButton_4,1);
    zoomStyleGrop->addButton(ui->pushButton_5,2);

    sliderTimer = new QTimer(this);

    connect(sliderTimer,SIGNAL(timeout()),this,SLOT(on_sliderTimer_timeout()));
    connect(zoomBtGrop,SIGNAL(buttonClicked(int)),this,SLOT(zoomBtGropClickedId(int)));
    connect(speedBtGrop,SIGNAL(buttonClicked(int)),this,SLOT(speedBtGropClickedId(int)));
    connect(zoomStyleGrop,SIGNAL(buttonClicked(int)),this,SLOT(zoomStyleGropClickedId(int)));
}



cameraAdjust::~cameraAdjust()
{
    delete ui;
}

void cameraAdjust::sendInputZoom(uint32_t val)
{
    uint8_t InputZoomCmd[18] = {0X77,0XEE,0XFF,0XFF,0XFF,0XFF,0X06,0X00,0X00,0X00,0X01,0X03,0X00,0X00,0X00,0X00,0X00,0XEF};
    memcpy(&InputZoomCmd[12],&val,sizeof(val));
    InputZoomCmd[16] = CheckXOR(InputZoomCmd,16);
    port->write((char *)InputZoomCmd,sizeof(InputZoomCmd));

}

void cameraAdjust::sendInputFocus(uint32_t val)
{
    uint8_t InputFocusCmd[18] = {0X77,0XEE,0XFF,0XFF,0XFF,0XFF,0X06,0X00,0X00,0X00,0X03,0X03,0X00,0X00,0X00,0X00,0X00,0XEF};
    memcpy(&InputFocusCmd[12],&val,sizeof(val));
    InputFocusCmd[16] = CheckXOR(InputFocusCmd,16);
    port->write((char *)InputFocusCmd,sizeof(InputFocusCmd));
}

void cameraAdjust::updateSysData()
{
    uint8_t speedState[3];
    memset(speedState,0,sizeof(speedState));
    float temp = (float)sysData.now.temp - 40;
    uint32_t now_m1_tick = sysData.now.now_m1_tick;
    uint32_t now_m2_tick = sysData.now.now_m2_tick;
    uint32_t max_m1_tick = sysData.now.max_m1_tick;
    uint32_t max_m2_tick = sysData.now.max_m2_tick;
    QString zoomLocation;
    QString focusLocation;
    zoomLocation = QString("%1/%2").arg(QString::number(now_m2_tick)).arg(QString::number(max_m2_tick));
    focusLocation = QString("%1/%2").arg(QString::number(now_m1_tick)).arg(QString::number(max_m1_tick));
  //  ui->lineEdit->setPlaceholderText(QString::number(sysData.now.zoom));               //变焦
  //  ui->lineEdit_2->setPlaceholderText(QString::number(sysData.now.focus));            //调焦
    ui->lineEdit_5->setText(zoomLocation);
    ui->lineEdit_6->setText(focusLocation);
    ui->lineEdit_3->setText(listSpeed.at(sysData.now.speed));
    ui->lineEdit_7->setText(QString("%1℃").arg(temp));
    speedState[sysData.now.speed] = 0X01;
    ui->radioButton_6->setChecked(speedState[0]);
    ui->radioButton_7->setChecked(speedState[1]);
    ui->radioButton_8->setChecked(speedState[2]);

    ui->label_19->setText(QString("变焦值:").append(QString::number(now_m2_tick)));
    ui->label_38->setText(QString("调焦值:").append(QString::number(now_m1_tick)));

}

void cameraAdjust::zoomBtGropClickedId(int id)
{
    uint8_t zoomSwCmd[14] = {0X77,0XEE,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X02,0X02,0X00,0X00,0XEF};
    zoomSwCmd[11] = id;
    zoomSwCmd[12] = CheckXOR(zoomSwCmd, 12);
    port->write((char *)zoomSwCmd,sizeof(zoomSwCmd));
    emit WaitMs(200);
    ui->lineEdit->setPlaceholderText(QString::number(sysData.flash.zoom_adjust[id]));
}

void cameraAdjust::speedBtGropClickedId(int id)
{
    uint8_t speedSwCmd[14] = {0X77,0XEE,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X02,0X04,0X00,0X9F,0XEF};
    speedSwCmd[11] = id;
    speedSwCmd[12] = CheckXOR(speedSwCmd, 12);
    port->write((char *)speedSwCmd,sizeof(speedSwCmd));
    emit WaitMs(200);
}


//小中大视场切换
void cameraAdjust::zoomStyleGropClickedId(int id)
{
    int a = 1 + 2* id;
    uint8_t state[5];
    memset(state,0x00,sizeof(state));
    state[a - 1] = 0X01;
    ui->radioButton->setChecked(state[0]);
    ui->radioButton_2->setChecked(state[1]);
    ui->radioButton_3->setChecked(state[2]);
    ui->radioButton_4->setChecked(state[3]);
    ui->radioButton_5->setChecked(state[4]);
    uint8_t zoomSwCmd[14] = {0X77,0XEE,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X02,0X02,0X00,0X00,0XEF};
    zoomSwCmd[11] = a - 1;
    zoomSwCmd[12] = CheckXOR(zoomSwCmd, 12);
    port->write((char *)zoomSwCmd,sizeof(zoomSwCmd));

    ui->lineEdit->setPlaceholderText(QString::number(sysData.flash.zoom_adjust[zoomSwCmd[11]]));

    emit WaitMs(200);
}



void cameraAdjust::on_pushButton_7_clicked()
{
    bool flag = false;
    QString Text = ui->lineEdit_8->placeholderText();
    int val =  Text.toInt(&flag);
    if(ui->lineEdit_8->placeholderText().isEmpty() || flag == false){
        QMessageBox::about(NULL,"错误","请输入变焦值,按回车键结束");
        return;
    }
    uint8_t zoomUpCmd[18] = {0X77,0XEE,0XFF,0XFF,0XFF,0XFF,0X06,0X00,0X00,0X00,0X01,0X01,0X00,0X00,0X00,0X00,0X00,0XEF};
    memcpy(&zoomUpCmd[12],&val,4);
    zoomUpCmd[16] = CheckXOR(zoomUpCmd,16);
    port->write((char *)zoomUpCmd,sizeof(zoomUpCmd));

    if((sysData.now.now_m2_tick + val) >= sysData.now.max_m2_tick){
        val = sysData.now.max_m2_tick;
    }else{
        val += sysData.now.now_m2_tick;
    }
    ui->lineEdit->setPlaceholderText(QString::number(val));
    emit WaitMs(200);
}

void cameraAdjust::on_pushButton_6_clicked()
{
    bool flag = false;
    QString Text = ui->lineEdit_8->placeholderText();
    int val =  Text.toInt(&flag);
    if(ui->lineEdit_8->placeholderText().isEmpty() || flag == false){
        QMessageBox::about(NULL,"错误","请输入变焦值,按回车键结束");
        return;
    }
    uint8_t zoomUpCmd[18] = {0X77,0XEE,0XFF,0XFF,0XFF,0XFF,0X06,0X00,0X00,0X00,0X01,0X02,0X00,0X00,0X00,0X00,0X00,0XEF};
    memcpy(&zoomUpCmd[12],&val,4);
    //qDebug() << "理应到达的tick = " << sysData.now.now_m2_tick - val << endl;
    zoomUpCmd[16] = CheckXOR(zoomUpCmd,16);
    port->write((char *)zoomUpCmd,sizeof(zoomUpCmd));
    if((unsigned int)val >=  sysData.now.now_m2_tick){
        val = 0;
    }else{
        val  = sysData.now.now_m2_tick - val;
    }
    ui->lineEdit->setPlaceholderText(QString::number(val));
    emit WaitMs(200);
}

void cameraAdjust::on_pushButton_clicked(bool checked)
{
    uint8_t zoomContinuousUpCmd[15] = {0X77,0XEE,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X03,0X07,0X01,0X01,0X9D,0XEF};
    uint8_t zoomContinuousStopCmd[14] = {0X77,0XEE,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X02,0X07,0X02,0X9E,0XEF};
    ui->pushButton_2->setEnabled(!checked);
    if(checked == true){
        port->write((char *)zoomContinuousUpCmd,sizeof(zoomContinuousUpCmd));
        ui->pushButton->setText("停止");
    }else{
        port->write((char *)zoomContinuousStopCmd,sizeof(zoomContinuousStopCmd));
        ui->pushButton->setText("连续变焦+");
    }
}

void cameraAdjust::on_pushButton_2_clicked(bool checked)
{
    uint8_t zoomContinuousDownCmd[15] = {0X77,0XEE,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X03,0X07,0X01,0X02,0X9E,0XEF};
    uint8_t zoomContinuousStopCmd[14] = {0X77,0XEE,0XFF,0XFF,0XFF,0XFF,0X00,0X00,0X00,0X02,0X07,0X02,0X9E,0XEF};
     ui->pushButton->setEnabled(!checked);
     if(checked == true){
         port->write((char *)zoomContinuousDownCmd,sizeof(zoomContinuousDownCmd));
         ui->pushButton_2->setText("停止");
     }else{
         port->write((char *)zoomContinuousStopCmd,sizeof(zoomContinuousStopCmd));
         ui->pushButton_2->setText("连续变焦-");
     }
}


//细调焦+
void cameraAdjust::on_pushButton_10_clicked()
{
    uint8_t focusPrecisionUpCmd[19] = {0X77,0XEE,0XFF,0XFF,0XFF,0XFF,0X07,0X00,0X00,0X03,0X03,0X02,0X01,0X00,0X00,0X00,0X00,0X00,0XEF};
    bool flag = false;
    QString Text = ui->lineEdit_9->placeholderText();
    int val =  Text.toInt(&flag);
    if(ui->lineEdit_9->placeholderText().isEmpty() || flag == false){
        QMessageBox::about(NULL,"错误","请输入变焦值,按回车键结束");
        return;
    }
    memcpy(&focusPrecisionUpCmd[13],&val,sizeof(val));
    focusPrecisionUpCmd[17] = CheckXOR(focusPrecisionUpCmd,17);
    focusPrecisionUpCmd[18] = 0XEF;
    port->write((char *)focusPrecisionUpCmd,sizeof(focusPrecisionUpCmd));

    if((sysData.now.now_m1_tick + val) >= sysData.now.max_m1_tick){
        val = sysData.now.max_m1_tick;
    }else{
        val += sysData.now.now_m1_tick;
    }
    ui->lineEdit_2->setPlaceholderText(QString::number(val));
    emit WaitMs(200);
}
//细调焦-
void cameraAdjust::on_pushButton_11_clicked()
{
    uint8_t focusPrecisionDownCmd[19] = {0X77,0XEE,0XFF,0XFF,0XFF,0XFF,0X07,0X00,0X00,0X00,0X03,0X02,0X02,0X00,0X00,0X00,0X00,0X00,0XEF};
    bool flag = false;
    QString Text = ui->lineEdit_9->placeholderText();
    int val =  Text.toInt(&flag);
    if(ui->lineEdit_9->placeholderText().isEmpty() || flag == false){
        QMessageBox::about(NULL,"错误","请输入变焦值,按回车键结束");
        return;
    }
    memcpy(&focusPrecisionDownCmd[14],&val,sizeof(val));
    focusPrecisionDownCmd[17] = CheckXOR(focusPrecisionDownCmd,17);
    focusPrecisionDownCmd[18] = 0XEF;
    port->write((char *)focusPrecisionDownCmd,sizeof(focusPrecisionDownCmd));

    if((unsigned int )val >= sysData.now.now_m1_tick){
        val = 0;
    }else{
        val = sysData.now.now_m1_tick - val;
    }
    ui->lineEdit_2->setPlaceholderText(QString::number(val));
    emit WaitMs(200);
}





void cameraAdjust::on_lineEdit_returnPressed()
{
    QString text = ui->lineEdit->text();
    if(textCheck(text,sysData.now.max_m2_tick) == false){
        text.clear();
    }
    if(text.isEmpty() == false ){
        sendInputZoom(text.toUInt());
    }
     ui->lineEdit->setPlaceholderText(text);
     ui->lineEdit->setText("");
     emit WaitMs(200);
}

void cameraAdjust::on_lineEdit_2_returnPressed()
{
    QString text = ui->lineEdit_2->text();

    if(textCheck(text,sysData.now.max_m1_tick) == false){
        text.clear();
    }
    //ui->lineEdit_2->setText(text);
    if(text.isEmpty() == false){
        sendInputFocus(text.toUInt());
    }
    ui->lineEdit_2->setPlaceholderText(text);
    ui->lineEdit_2->setText("");
    emit WaitMs(200);
}

void cameraAdjust::on_lineEdit_8_returnPressed()
{
    QString text = ui->lineEdit_8->text();
    if(textCheck(text,sysData.now.max_m2_tick) == false){
        text.clear();
    }
    ui->lineEdit_8->setPlaceholderText(text);
    ui->lineEdit_8->setText("");
}


void cameraAdjust::on_horizontalSlider_valueChanged(int value)
{
    sliderTimer->start(300);
}

void cameraAdjust::on_sliderTimer_timeout()
{
    sliderTimer->stop();
    int value = ui->horizontalSlider->value();
    int ZoomVal = sysData.now.max_m2_tick * value / 100;
    ui->lineEdit->setPlaceholderText(QString::number(ZoomVal));
     sendInputZoom(ZoomVal);
     emit WaitMs(200);
}

void cameraAdjust::on_lineEdit_9_returnPressed()
{
    QString text = ui->lineEdit_9->text();
    if(textCheck(text,sysData.now.max_m1_tick) == false){
        text.clear();
    }
    ui->lineEdit_9->setPlaceholderText(text);
    ui->lineEdit_9->setText("");
}



